package com.atguigu.srb.core.controller.admin;


import com.atguigu.srb.common.exception.Assert;
import com.atguigu.srb.common.exception.BusinessException;
import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.common.result.Result;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author lsyandwh
 * @since 2022-12-02
 */
@Api(tags = "积分等级管理")
@RestController
//@CrossOrigin//解决跨域问题
@RequestMapping("/admin/core/integralGrade")
public class AdminIntegralGradeController {

    @Autowired
    private IntegralGradeService integralGradeService;

    @ApiOperation(value = "查询所有等级信息")
    @GetMapping("/list")
    public Result gteList(){

        List<IntegralGrade> list = integralGradeService.list();
        return Result.ok().data("list",list);
    }

    @ApiOperation("添加积分等级")
    @PostMapping("/save")
    public Result save(@RequestBody IntegralGrade integralGrade){

//        Assert.notNull(integralGrade.getIntegralEnd(),ResponseEnum.BORROW_AMOUNT_NULL_ERROR);

        Integer integralEnd = integralGrade.getIntegralEnd();
        Integer integralStart = integralGrade.getIntegralStart();
        BigDecimal borrowAmount = integralGrade.getBorrowAmount();

        if(integralEnd <= 0 || integralStart <= 0 || borrowAmount.intValue() <= 0){
            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        }

        boolean save = integralGradeService.save(integralGrade);
        return Result.ok().data("b",save);
    }

    @ApiOperation("修改积分等级")
    @PutMapping("/edit")
    public Result editById(@RequestBody IntegralGrade integralGrade){

        boolean b = integralGradeService.updateById(integralGrade);
        return Result.ok().data("b",b);
    }

    @ApiOperation("根据主键id删除积分等级")
    @DeleteMapping("/remove/{id}")
    public Result removeById(@ApiParam("主键id") @PathVariable Long id){

        boolean b = integralGradeService.removeById(id);
        return Result.ok().data("b",b);
    }

    @ApiOperation("根据主键id查询积分等级")
    @GetMapping("/getById/{id}")
    public Result getById(@ApiParam("主键id") @PathVariable Long id){
        IntegralGrade integralGrade = integralGradeService.getById(id);
        return Result.ok().data("integralGrade",integralGrade);
    }

}

